/**
 * The base for Multichannel JavaScript
 * requires mc.base.js
 */
 
 ( function( $ ) {
 
 	MC.storeSelector = {
			
		locationValues : {},

		getValues : function(){
			return '{ "StoreSelectorForm_SearchTextField" : "' + $('#StoreSelectorForm_SearchTextField').val() + '" , ' +			
			'"StoreSelectorForm_Latitude" : "' + $('#StoreSelectorForm_Latitude').val() + '" , ' +
			'"StoreSelectorForm_Longitude" : "' + $('#StoreSelectorForm_Longitude').val() + '" }'								
		},

		setLocationValues : function(jsonObj){
			$.each( jsonObj , function( key, value ){
				if ( $(key) ){
					$('#'+key).val( value );
				}
			})
		},

		storeSearchFormAction : function( id ){
			return $("#" + id ).attr("action")
		},

		getLatLong : function( nextFunction ){
			if ($("#AdvancedSearch").val()=='false') {
				$("#StoreSelectorForm_Latitude").val(0);
				$("#StoreSelectorForm_Longitude").val(0);
						nextFunction();
				});				
			} else {
				geocoder = new google.maps.Geocoder();			
				 geocoder.geocode( { 'address' : $("#StoreSelectorForm_SearchTextField").val() }, function(results, status) {
				      if (status == google.maps.GeocoderStatus.OK) {
				    	    $("#StoreSelectorForm_Latitude").val(results[0].geometry.location.lat());
							$("#StoreSelectorForm_Longitude").val(results[0].geometry.location.lng());
							nextFunction();
				      } else {
				        alert("Geocode was not successful for the following reason: " + status);
				      }
				    });
			}
		},

		getNextStoreLocations : function(){			
			$("#StoreSelectorForm_PageIndex").val(Number($("#StoreSelectorForm_PageIndex").val())+Number($("#StoreSelectorForm_PageSize").val()));
			MC.storeSelector.getStoreLocations();
		},

		getPrevStoreLocations : function(){
			var prevIndex = Math.max(0,Number($("#StoreSelectorForm_PageIndex").val())-Number($("#StoreSelectorForm_PageSize").val()));			
			$("#StoreSelectorForm_PageIndex").val(prevIndex);
			MC.storeSelector.getStoreLocations();
		},

		getStoreLocations : function(){
			$.ajax({
				type: "GET",
				
				url: MC.storeSelector.requestStores( $("#search-stores").attr("action") ),
				dataType: "html",				
				success: function( html  ){
					$("#store-results").html( html )
				}
			});
		},

		requestStores : function( baseURL ){
			var storeDistance = "?StoreSelectorForm_Distance="+ $("#StoreSelectorForm_Distance").val() +"",
			storeDistanceUnit = "&StoreSelectorForm_DistanceUnit="+ $("#StoreSelectorForm_DistanceUnit").val() +"",
			searchField = "&StoreSelectorForm_SearchTextField="+ $("#StoreSelectorForm_SearchTextField").val() +"",
			local = "&StoreSelectorForm_Locale="+ $("#StoreSelectorForm_Locale").val() +"",
			lat = "&StoreSelectorForm_Latitude="+ $("#StoreSelectorForm_Latitude").val() +"",
			long = "&StoreSelectorForm_Longitude="+ $("#StoreSelectorForm_Longitude").val() +"",
			pageIndex = "&StoreSelectorForm_PageIndex="+ $("#StoreSelectorForm_PageIndex").val() +"",
			partner = "&StoreSelectorForm_Partner="+ $("#StoreSelectorForm_Partner").val() +"",
			pageSize = "&StoreSelectorForm_PageSize="+ $("#StoreSelectorForm_PageSize").val() +"",
			productSKU= "&ProductSKU="+$("#StoreSelectorForm_ProductSKU").val() + "",		
			lineItem = "&ProductLineItemUUID="+$("#ProductLineItemUUID").val()+"",
			quantity = "&StoreSelectorForm_Quantity=" + MC.storeSelector.requestedQuantity + "",
			jumpTarget= "&JumpTarget=ViewStoreSelector-FindStores",
			configPagelet= "&ConfigurationPageletUUID="+$("#ConfigurationPageletUUID").val()+"",			
			findStores = "&FindStores=+Find+Stores+";
			return baseURL + storeDistance + storeDistanceUnit + searchField + local + lat + long + pageIndex + partner + pageSize + lineItem + quantity + jumpTarget + productSKU + configPagelet+ findStores;
		},
		
		requestedQuantity: "1",
		
		loadCookies: function(){
			
			$(document).ajaxComplete( function() {
				if ($("#cookies-loaded").val()=='false') {
					$("#cookies-loaded").val('true');
					MC.utils._cookie.get( "MC");
					MC.storeSelector.setLocationValues(MC.storeSelector.locationValues);
					if ($("#StoreSelectorForm_SearchTextField").val()!='' && $("#PerformAutoSearch").val()=='true') {
						MC.storeSelector.getStoreLocations();
					}
				}
			}) }

	};
 
 })( jQuery );